<script setup lang="ts">
import TableView from '@/components/TableView.vue'
import {
  NButton,
  NDataTable,
  NForm,
  NFormItem,
  NPagination,
  useMessage,
  type DataTableColumns,
  NSpace,
  NInput,
  NSelect
} from 'naive-ui'
import { h, ref, reactive, watch } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import { useViewerStore } from '../Common/store'

const message = useMessage()
const route = useRoute()
const useStore = useViewerStore()

const rowKey: any = (e: any) => e.id || ''
const columns: DataTableColumns = [
  {
    type: 'selection',
    width: '5rem'
  },
  {
    title: '员工编号',
    key: 'userNumber',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '姓名',
    key: 'userName',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '部门',
    key: 'deptName',
    width: '30rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '200rem'
        }
      }
    }
  },
  {
    title: '手机号',
    key: 'phone',
    width: '20rem',
    align: useStore.isCenter
  },
  {
    title: '身份证号',
    key: 'idNumber',
    width: '20rem',
    align: useStore.isCenter
  },
  {
    title: '性别',
    key: 'sex',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '电子邮箱',
    key: 'mail',
    width: '20rem',
    align: useStore.isCenter
  },
  {
    title: '删除时间',
    key: 'delTime',
    width: '20rem',
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    width: '10rem',
    align: useStore.isCenter,
    fixed: 'right',
    render: (e: any) => [
      h(
        NButton,
        { type: 'primary', quaternary: true, onClick: () => handleBack(e) },
        {
          default: () => '恢复'
        }
      )
    ]
  }
]

//筛选条件
const name: any = ref()
const deptId: any = ref()

const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    queryTableData()
  }
})

//部门类型
const partList: any = ref([])
const getPartName = async () => {
  const [error, res] = await sendRequest<HttpResponse>({
    module: 'overTime',
    api: 'BbgDeptList'
  })
  if (error) return message.error(error.message)
  partList.value = res.data
}
getPartName()

//恢复
const handleBack = async (val: any) => {
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'userDeleteBack',
    override: {
      params: {
        id: val?.id
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  message.success(res.data)
  queryTableData(true)
}

//多选框勾选
type DataListSelectionsItem = {
  id: string
}
const dataListSelections: any = ref<DataListSelectionsItem[]>([])
function selectionChangeHandle(val: any) {
  dataListSelections.value = val
}

//表格数据
const tableLoading = ref(false)
const tableData: any = ref([])
const queryTableData = async (resetPage = false) => {
  if (resetPage) paginationReactive.page = 1
  tableLoading.value = true
  dataListSelections.value = []
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'userDeleteList',
    override: {
      params: {
        name: name.value,
        deptId: deptId.value,
        page: paginationReactive.page,
        limit: paginationReactive.pageSize
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  paginationReactive.itemCount = res.data?.totalCount
  const tmp: any = res.data
  tableData.value = tmp?.list
}
watch(
  () => route.path,
  () => {
    if (route.path == '/OA/Office/UserDeleted') queryTableData(queryTableData)
  },
  { deep: true, immediate: true }
)
</script>

<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NForm :show-feedback="false" label-placement="left" inline>
          <NFormItem label="姓名:">
            <NInput placeholder="请输入姓名" v-model:value="name" />
          </NFormItem>
          <NFormItem label="部门:">
            <NSelect :options="partList" v-model:value="deptId" label-field="name" value-field="value" placeholder="请选择部门" clearable style="width: 200px" />
          </NFormItem>
          <NFormItem>
            <NButton type="primary"  ghost @click="queryTableData()" :loading="tableLoading">
              查询
            </NButton>
          </NFormItem>
        </NForm>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :row-key="rowKey" :columns="columns" :data="tableData"
        :scroll-x="1800" :loading="tableLoading" :checked-row-keys="dataListSelections"
        @update:checked-row-keys="selectionChangeHandle" />
    </template>

    <template #pagination>
        <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>
</template>

<style scoped lang="scss">
.table {
  height: 100%;
  padding: 29px 30px 42px;
}
</style>
